//
//  ViewController.swift
//  FastV
//
//  Created by DC on 2024/6/24.
//

import UIKit

class MainController: UIViewController {
    
    @IBOutlet var buttons: [UIButton]!
    @IBOutlet var tabs: [UIButton]!

    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var tableView: UITableView!
    
<<<<<<< HEAD
    private  lazy var tasks: [TaskModel] = YYGTaskManager.shared.getTasks()
    private lazy var  notes:  [NoteModel] = YYGNoteManager.shared.getNotes()
    
    @IBOutlet weak var topLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        topLabel.text = "Your \nWork Tasks(\(tasks.count))"
=======
    @IBOutlet weak var topLabel: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        topLabel.text = "Your \nWork Tasks(24)"
>>>>>>> c1d8770387c83a01c6645dbb2f22ec7e766582c2
//        CubeCell
        //collocetionView
        
        //收藏collectionView设置
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
//        中间还有两个间隙 = 8
        let gap: CGFloat = 5
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        // 两间隙 = 15
        let margin: CGFloat = 30
        let itemWith = (kScreenWidth - 2*margin - 3*gap)/4
        layout.itemSize = CGSize(width: itemWith, height: itemWith + 10)
        collectionView.collectionViewLayout =  layout
        collectionView.register(UINib(nibName: "CubeCell", bundle: nil), forCellWithReuseIdentifier: CubeCell.identifier)
        collectionView.contentInset = UIEdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.bounces = false
        collectionView.backgroundColor = UIColor.clear
        
        NotificationCenter.default.addObserver(self, selector: #selector(refreshTasks), name: Notification.Name(Notice.kTaskChange), object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(refreshNodes), name: Notification.Name(Notice.kNoteChange), object: nil)
        
        
     
//      tableView.separatorInset = UIEdgeInsets(top: 0.5, left:15, bottom: 0.5, right: 15)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.clear
        tableView.register(UINib(nibName: "RectCell", bundle: nil), forCellReuseIdentifier: RectCell.identifier)
        tableView.contentInsetAdjustmentBehavior = .never
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        }
        let footerView  =  UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 50))
        tableView.tableFooterView = footerView

    }

    @IBAction func addClick(_ sender: UIButton) {
        let addVc = AddTaskController()
        addVc.modalPresentationStyle = .fullScreen
        present(addVc, animated: true)
    }
    
    //Mark: -- 刷新任务列表
    @objc private func refreshTasks(){
        tasks = YYGTaskManager.shared.getTasks()
        topLabel.text = "Your \nWork Tasks(\(tasks.count))"
        collectionView.reloadData()
    }
    
    //Mark: -- 刷新任务列表
    @objc private func refreshNodes(){
        notes = YYGNoteManager.shared.getNotes()
        topLabel.text = "Your \nWork Notes(\(notes.count))"
        tableView.reloadData()
    }
    @IBAction func buttonClick(_ sender: UIButton) {
        switch sender.tag {
        case 300:
            collectionView.isHidden = false
            tableView.isHidden = true
            topLabel.text = "Your \nWork Tasks(\(tasks.count))"
        case 301:
            collectionView.isHidden = true
            tableView.isHidden = false
            topLabel.text = "Your \nWork Notes(\(notes.count))"
        default:
            break
        }
        for b in tabs {
            if b == sender {
                b.isSelected = true
            }else {
                b.isSelected = false
            }
        }
    }
}



// MARK: -- Collection的代理，数据源（扩展）
extension MainController: UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//        return modules.count
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CubeCell.identifier, for: indexPath) as! CubeCell
//        let module = modules[indexPath.item]
//        cell.module = module
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: false)
//        let module = modules[indexPath.item]
//        delegate?.didClick(module: module)
        let vc = TaskDetailController()
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true)
        
    }
}


// MARK: -- Collection的代理，数据源（扩展）
extension MainController: UITableViewDelegate , UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return notes.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let note = notes[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: RectCell.identifier, for: indexPath) as! RectCell
        cell.note = note
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let note = notes[indexPath.row]
        let vc = NoteDetailController()
        vc.note = note
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true)
    }

}
